package com.wu01.demo.controller;

import com.wu01.demo.Entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @PostMapping("/login")
    public String login(User user, HttpSession session, Model model){
        System.out.println(user);


        if(user != null){
            session.setAttribute("user",user);
            model.addAttribute("msg","login success");
//            model.addAttribute("users",users);
            return "redirect:/index";
        }else{
            return "badfdfsdfafaaagin1111";
        }
    }

    @GetMapping(value = {"/","/login"})
    public String login(){
        return "badfdfsdfafaaagin1111";
    }

    @GetMapping(value = {"/index"})
    public String index(HttpSession session, Model model){
        if(session.getAttribute("user") != null){
            return "index";
        }else{
            model.addAttribute("msg","no user") ;
            return "badfdfsdfafaaagin1111";
        }
    }

}
